from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import date

from apps.users.models import User
class DailyActiveUsersCountView(APIView):
    def get(self,request):
        today=date.today()
        count = User.objects.filter(last_login__gte=today).count()
        data = {
            'count':count,
            'date':today
        }
        return Response(data)


class DailyOrderUsersCountView(APIView):
    def get(self,request):
        today = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=today).count()
        data = {
            'count': count,
            'date': today
        }
        return Response(data)


from datetime import timedelta
class MonthIncrementUsersCountView(APIView):
    def get(self,request):
        data= []
        today = date.today()
        start_date = today-timedelta(days=30)
        for i in range(30):
            i_date = start_date+timedelta(days=i)
            c_date = start_date+timedelta(days=i+1)

            count = User.objects.filter(date_joined__gte=i_date, date_joined__lt=c_date).count()
            data.append({
                'count': count,
                'date': i_date
            })
        return Response(data)


class UserCountView(APIView):
    def get(self,request):
        count = User.objects.all().count()
        today = date.today()
        data = {
            'count':count,
            'date':today
        }
        return Response(data)


class DailyIncrementUsersCountView(APIView):
    def get(self,request):
        today= date.today()
        count = User.objects.filter(date_joined__gte=today).count()
        data = {
            'count': count,
            'date': today
        }
        return Response(data)

